package com.yzz.hzraisedust.hzraseidust.main;


import android.app.Activity;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.app.Fragment;
import android.os.Bundle;
import android.view.KeyEvent;

import com.ashokvarma.bottomnavigation.BottomNavigationBar;
import com.ashokvarma.bottomnavigation.BottomNavigationItem;
import com.yzz.hzraisedust.hzraseidust.R;

import java.util.Map;

public class InfoActivity extends BaseActivity implements BottomNavigationBar.OnTabSelectedListener{

    private BottomNavigationBar mBottomNavigationBar;
    //地图
    private MapInfoFragment mMapInfoFragment;
    //预警
    private  Fragment mAlarmFragment;
    private Fragment mStaticsFragment;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_info);

        initView();
        initLoginingDlg();

        setDefaultFragment();
    }

    private void initView() {
        mBottomNavigationBar  = findViewById(R.id.bottom_navigation_bar);
        mBottomNavigationBar
                .addItem(new BottomNavigationItem(R.mipmap.home, getResources().getString(R.string.home)))
                .addItem(new BottomNavigationItem(R.mipmap.alarm, getResources().getString(R.string.alarm)))
                .addItem(new BottomNavigationItem(R.mipmap.statistics ,getResources().getString(R.string.statistics)))
               .initialise();
        mBottomNavigationBar.setTabSelectedListener(this);
    }
    /**
     * 设置默认的
     */
    private void setDefaultFragment() {
        //setTitle(getResources().getString(R.string.home));
        FragmentManager fm = getFragmentManager();
        FragmentTransaction transaction = fm.beginTransaction();

        mMapInfoFragment = MapInfoFragment.getInstance();

        transaction.replace(R.id.container_fragment, mMapInfoFragment);
        transaction.commit();
    }

    private void hideFragment(FragmentTransaction transaction){
        if(mMapInfoFragment!=null){
            transaction.hide(mMapInfoFragment);
        }
        if(mAlarmFragment != null){
            transaction.hide(mAlarmFragment);
        }
        if(mStaticsFragment != null){
            transaction.hide(mStaticsFragment);
        }
    }
    @Override
    public void onTabSelected(int i) {
        FragmentManager fm = this.getFragmentManager();
        //开启事务
        FragmentTransaction transaction = fm.beginTransaction();
        hideFragment(transaction);
        switch (i) {
            case 0:
                setTitle(getResources().getString(R.string.home));
                if (mMapInfoFragment == null) {
                    mMapInfoFragment = MapInfoFragment.getInstance();
                    transaction.add(R.id.container_fragment,mMapInfoFragment);
                }else {
                   // transaction.replace(R.id.container_fragment, mMapInfoFragment);
                }
                transaction.show(mMapInfoFragment);
                break;
            case 1:
                setTitle(getResources().getString(R.string.alarm));
                if (mAlarmFragment == null) {
                    mAlarmFragment = AlarmFragment.getInstance();
                    transaction.add(R.id.container_fragment,mAlarmFragment);
                }
               // transaction.replace(R.id.container_fragment, mAlarmFragment);
                transaction.show(mAlarmFragment);
                break;
            case 2:
                setTitle(getResources().getString(R.string.statistics));
                if (mStaticsFragment == null) {
                    mStaticsFragment = StasticsFragment.getInstance();
                    transaction.add(R.id.container_fragment,mStaticsFragment);
                }
               // transaction.replace(R.id.container_fragment, mStaticsFragment);
                transaction.show(mStaticsFragment);
                break;

            default:
                break;
        }
        // 事务提交
        transaction.commit();

    }

    @Override
    public void onTabUnselected(int i) {

    }

    @Override
    public void onTabReselected(int i) {

    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {

        if(keyCode == KeyEvent.KEYCODE_BACK){
            finish();
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }
}
